package com.ruoyi.demo.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.mapper.AuctionFavoriteMapper;
import com.ruoyi.demo.domain.AuctionFavorite;
import com.ruoyi.demo.domain.dto.AuctionFavoriteWithGoodsDTO;
import com.ruoyi.demo.service.IAuctionFavoriteService;

/**
 * 收藏Service业务层处理
 * 
 * @author metaee
 * @date 2025-02-24
 */
@Service
public class AuctionFavoriteServiceImpl implements IAuctionFavoriteService 
{
    @Autowired
    private AuctionFavoriteMapper auctionFavoriteMapper;

    /**
     * 查询收藏
     * 
     * @param favoriteId 收藏主键
     * @return 收藏
     */
    @Override
    public AuctionFavorite selectAuctionFavoriteByFavoriteId(Long favoriteId)
    {
        return auctionFavoriteMapper.selectAuctionFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 查询收藏列表
     * 
     * @param auctionFavorite 收藏
     * @return 收藏
     */
    @Override
    public List<AuctionFavorite> selectAuctionFavoriteList(AuctionFavorite auctionFavorite)
    {
        return auctionFavoriteMapper.selectAuctionFavoriteList(auctionFavorite);
    }

    /**
     * 新增收藏
     * 
     * @param auctionFavorite 收藏
     * @return 结果
     */
    @Override
    public int insertAuctionFavorite(AuctionFavorite auctionFavorite)
    {
        auctionFavorite.setCreateTime(DateUtils.getNowDate());
        return auctionFavoriteMapper.insertAuctionFavorite(auctionFavorite);
    }

    /**
     * 修改收藏
     * 
     * @param auctionFavorite 收藏
     * @return 结果
     */
    @Override
    public int updateAuctionFavorite(AuctionFavorite auctionFavorite)
    {
        auctionFavorite.setUpdateTime(DateUtils.getNowDate());
        return auctionFavoriteMapper.updateAuctionFavorite(auctionFavorite);
    }

    /**
     * 批量删除收藏
     * 
     * @param favoriteIds 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteAuctionFavoriteByFavoriteIds(Long[] favoriteIds)
    {
        return auctionFavoriteMapper.deleteAuctionFavoriteByFavoriteIds(favoriteIds);
    }

    /**
     * 删除收藏信息
     * 
     * @param favoriteId 收藏主键
     * @return 结果
     */
    @Override
    public int deleteAuctionFavoriteByFavoriteId(Long favoriteId)
    {
        return auctionFavoriteMapper.deleteAuctionFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 根据用户ID查询收藏列表（包含商品详情）
     * 
     * @param userId 用户ID
     * @return 收藏与商品详情集合
     */
    @Override
    public List<AuctionFavoriteWithGoodsDTO> selectFavoriteWithGoodsByUserId(Long userId)
    {
        return auctionFavoriteMapper.selectFavoriteWithGoodsByUserId(userId);
    }

    /**
     * 根据用户ID和商品ID查询是否已收藏
     * 
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return 是否已收藏
     */
    @Override
    public boolean isFavorite(Long userId, Long goodsId) {
        AuctionFavorite favoriteQuery = new AuctionFavorite();
        favoriteQuery.setUserId(userId);
        favoriteQuery.setGoodsId(goodsId);
        List<AuctionFavorite> list = auctionFavoriteMapper.selectAuctionFavoriteList(favoriteQuery);
        return list != null && !list.isEmpty();
    }

    /**
     * 根据用户ID和商品ID查询收藏信息
     * 
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return 收藏信息（包含是否收藏和收藏ID）
     */
    @Override
    public Map<String, Object> getFavoriteInfo(Long userId, Long goodsId) {
        Map<String, Object> result = new HashMap<>();
        
        AuctionFavorite favoriteQuery = new AuctionFavorite();
        favoriteQuery.setUserId(userId);
        favoriteQuery.setGoodsId(goodsId);
        List<AuctionFavorite> list = auctionFavoriteMapper.selectAuctionFavoriteList(favoriteQuery);
        
        boolean isFavorite = list != null && !list.isEmpty();
        result.put("isFavorite", isFavorite);
        
        if (isFavorite) {
            result.put("favoriteId", list.get(0).getFavoriteId());
        }
        
        return result;
    }
}
